/*************************************************************************
 * gOkey - A tile based game for Gnome.                                  *
 * Copyright (C) 2008  Onur Aslan  <onuraslan@gmail.com>                 *
 *                                                                       *
 * This program is free software: you can redistribute it and/or modify  *
 * it under the terms of the GNU General Public License as published by  *
 * the Free Software Foundation, either version 3 of the License, or     *
 * (at your option) any later version.                                   *
 *                                                                       *
 * This program is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *************************************************************************/

#include "gui.h"


struct {
  int width;
  int height;
  struct {
    double ratio;
    int width;
    int height;
  } aspect;
  Position rack;
} table = {
  0, 0, { 0.0, 0, 0}, { 0, 0, 0, 0 }
};


gboolean
configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer data)
{

  /* calculating aspect ratio */
  if (table.aspect.ratio == 0.0) {
    table.aspect.ratio = 1.0;
    table.aspect.width = widget->allocation.width;
    table.aspect.height = widget->allocation.height;
  } else {
    table.aspect.ratio = (double) widget->allocation.width
                             / table.aspect.width;
  }

  table.width = widget->allocation.width;
  table.height = widget->allocation.height;
  
  /* setting rack variables */
  table.rack.width = table.aspect.ratio * RACKWIDTH;
  table.rack.height = table.aspect.ratio * RACKHEIGHT;

  return TRUE;
}

gboolean
expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  /* drawing background */
  gdk_draw_rectangle (widget->window,
                      widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
                      TRUE,
                      0, 0,
                      widget->allocation.width,
                      widget->allocation.height);
                      
  return TRUE;
}

GtkWidget *
create_board (void)
{
  board = gtk_drawing_area_new ();
  GTK_WIDGET_SET_FLAGS (board, GTK_CAN_FOCUS);
  gtk_widget_set_size_request (board, BOARDWIDTH, BOARDHEIGHT);
  gtk_widget_set_events (board, gtk_widget_get_events (board)
                                | GDK_LEAVE_NOTIFY_MASK
                                | GDK_BUTTON_PRESS_MASK
                                | GDK_BUTTON_RELEASE_MASK
                                | GDK_POINTER_MOTION_MASK
                                | GDK_POINTER_MOTION_HINT_MASK);
  g_signal_connect (G_OBJECT (board), "configure-event",
                    G_CALLBACK (configure_event), NULL);
  g_signal_connect (G_OBJECT (board), "expose-event",
                    G_CALLBACK (expose_event), NULL);
  return board;
}
